//package com.maikang.exammall.serializer;
//
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.databind.DeserializationContext;
//import com.fasterxml.jackson.databind.JsonDeserializer;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.maikang.exammall.util.JsonUtil;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.domain.Pageable;
//import org.springframework.data.domain.Sort;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//import java.util.*;
//
//public class PaginationDeserializer extends JsonDeserializer<Pageable> {
//    @Override
//    public Pageable deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
//        JsonNode jsonNode = deserializationContext.readTree(jsonParser);
//        if (jsonNode == null || jsonNode.get("page") == null || jsonNode.get("size") == null) {
//            return null;
//        }
//        int page = jsonNode.get("page").asInt();
//        int size = jsonNode.get("size").asInt();
//        JsonUtil jsonUtil = new JsonUtil(new ObjectMapper());
//        Sort sort;
//        JsonNode sortJsonNode = jsonNode.get("sort");
//        if (sortJsonNode == null) {
//            sort = Sort.unsorted();
//        } else {
//            List<String> directionParameters = jsonUtil.jsonToList(jsonNode.get("sort").toString(), String.class);
//            sort = parseParameterIntoSort(directionParameters, ",");
//        }
//
//        Pageable pageable = PageRequest.of(page, size);
//        return (Pageable) (sort.isSorted() ? PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort) : pageable);
//    }
//
//    private Sort resolveSortArgument(List<String> directionParameter) {
//        if (directionParameter == null || directionParameter.size() == 0) {
//            return Sort.unsorted();
//        }
//        if (directionParameter.size() == 1 && !StringUtils.hasText(directionParameter.get(0))) {
//            return Sort.unsorted();
//        }
//        return this.parseParameterIntoSort(directionParameter, ",");
//
//    }
//
//    private Sort parseParameterIntoSort(List<String> source, String delimiter) {
//        List<Sort.Order> allOrders = new ArrayList();
//        Iterator<String> iterator = source.iterator();
//        while (true) {
//            String part;
//            do {
//                if (!iterator.hasNext()) {
//                    return allOrders.isEmpty() ? Sort.unsorted() : Sort.by(allOrders);
//                }
//                part = iterator.next();
//            } while (part == null);
//            String[] elements = Arrays.stream(part.split(delimiter)).filter(this::notOnlyDots).toArray((x$0) -> {
//                return new String[x$0];
//            });
//            Optional<Sort.Direction> direction = elements.length == 0 ? Optional.empty() : Sort.Direction.fromOptionalString(elements[elements.length - 1]);
//            int lastIndex = direction.map((it) -> {
//                return elements.length-1;
//            }).orElseGet(() -> {
//                return elements.length;
//            });
//            for (int i = 0; i < lastIndex; ++i) {
//                toOrder(elements[i], direction).ifPresent(allOrders::add);
//            }
//        }
//    }
//
//    private static Optional<Sort.Order> toOrder(String property, Optional<Sort.Direction> direction) {
//        return !StringUtils.hasText(property) ? Optional.empty() : Optional.of(direction.map((it)->{
//            return new Sort.Order(it, property);
//        }).orElseGet(() -> {
//            return Sort.Order.by(property);
//        }));
//    }
//
//    private boolean notOnlyDots(String source) {
//        return StringUtils.hasText(source.replace(".", ""));
//    }
//}
